//
//  WZOutListController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 2016/9/29.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZOutListController.h"
#import "WZOutProceduleCell.h"
#import "WZOutProceduleList.h"

@interface WZOutListController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *outSignArray;
@property (nonatomic, copy) NSString *applyUrl;

@end

@implementation WZOutListController

static NSString *const identifier = @"outProcedureListCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpTableView];
    
    [self loadWorkFlowApplyList];
}


- (void)setUpTableView {
    
    self.tableView.backgroundColor = WhiteColor;
    self.navigationItem.title = @"外出签到列表";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WZOutProceduleCell class]) bundle:nil] forCellReuseIdentifier:identifier];
}


- (void)loadWorkFlowApplyList {
    
    [MBProgressHUD showMessage:@"正在加载..." toView:self.view];
    [NetWorkShare getWorkFlowWithApplyAndLoginName:self.oaUserName password:self.oaPwd companyId:self.oaCompanyID FormId:self.formId successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            WZLog(@"result = %@", responseObject);
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [MBProgressHUD hideHUDForView:self.view];
                [self.listArray removeAllObjects];
                self.listArray = [WZOutProceduleList objectArrayWithKeyValuesArray:responseObject[@"datas"]];
                self.applyUrl = responseObject[@"applyUrl"];
                
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
                
            } else {
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showError:[responseObject objectForKey:@"加载页面出错"] toView:self.view];
            }
        } else {
            
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showError:UNKNOWNERROR toView:self.view];
        }
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        WZLog(@"error = %@", error);
        
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:NETWORKNERROR toView:self.view];
    }];
}



#pragma mark -- dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZOutProceduleCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    cell.list = self.listArray[indexPath.row];
    return cell;
}


#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZOutProceduleList *list = self.listArray[indexPath.row];
    
    if ([list.runState isEqualToString:@"运行中"]) {
        
        // 外出打卡
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"userId"] = self.oaUserId;
        parameters[@"applyId"] = self.outId;
        parameters[@"address"] = self.address;
        parameters[@"typeFlag"] = self.signFlag;
        parameters[@"longitude"] = self.longitude;
        parameters[@"latitude"] = self.latitude;
        parameters[@"modelId"] = list.modelId;
        parameters[@"mversion"] = list.mversion;
        parameters[@"fversion"] = list.fversion;
        parameters[@"startEventkey"] = list.startEventKey;
        
        [NetWorkShare outSignOrSignOutWithProcedureParameters:parameters successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                BOOL isSuccess = YES;
                if (_block) {
                    _block(isSuccess);
                }
                
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                
                [MBProgressHUD hideHUDForView:self.view];
                [MBProgressHUD showError:[responseObject objectForKey:@"message"] toView:self.view];
            }
            
        } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showError:@"网络错误!" toView:self.view];
        }];
        
    } else {
        
        [MBProgressHUD showError:@"流程非运行中!" toView:self.view];
    }
}



#pragma mark - 懒加载

- (NSMutableArray *)listArray
{
    if (_listArray == nil) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (NSMutableArray *)outSignArray
{
    if (_outSignArray == nil) {
        _outSignArray = [NSMutableArray array];
    }
    return _outSignArray;
}



@end
